package com.light.controller;

import com.light.service.DownloadService;
import javax.swing.*;

/**
 * @Author MoonlightL
 * @ClassName: MyController
 * @ProjectName downloader
 * @Description: 控制器
 * @DateTime 2023/3/10, 0010 18:26
 */
public class MyController {

    private DownloadService downloadService;

    private volatile boolean started;


    public MyController() {
        this.downloadService = new DownloadService();
    }

    public void addTask(String url, String dir, JTextArea textArea) {
        if (started) {
            JOptionPane.showMessageDialog(null, "已开始下载文件，请勿重复点击");
            return;
        }

        started = true;

        new Thread(() -> {
            try {
                this.downloadService.downloadFile(url, dir, textArea);
                started = false;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }
}
